package de.pipeGame.playingField;

import de.pipeGame.Level;
import de.pipeGame.R;
import de.pipeGame.itemStack.ItemStackHolder;
import EventSourcing.EventQueue;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * The playingField containts an 2-dimensional Array of PlayingFieldItems to
 * handle the field.
 * 
 * @author Anthea
 * 
 */
public class PlayingField {

	private static int FIELD_WIDTH = 5;
	private static int FIELD_HEIGHT = 5;

	PlayingFieldItem field[][];
	PlayingFieldItem start; 
	PlayingFieldItem target;
	Level level;

	public PlayingField(Context context, ViewGroup layout,ItemStackHolder itemStackHolder, int itemHeight, int itemWidth, Level lvl, EventQueue eventQueue) {
		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		this.level = lvl;
		field = new PlayingFieldItem[FIELD_HEIGHT][FIELD_WIDTH];

		// init all the fields
		for (int i = 0; i < FIELD_HEIGHT; i++) {
			LinearLayout row = new LinearLayout(context);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			row.setLayoutParams(params);
			row.setOrientation(LinearLayout.HORIZONTAL);

			for (int j = 0; j < FIELD_WIDTH; j++) {
				ViewGroup vg = (ViewGroup) mInflater.inflate(
						R.layout.pipe_item, layout, false);
				field[i][j] = new PlayingFieldItem(j, i,
						(ImageView) vg.findViewById(R.id.image),
						itemStackHolder, itemHeight, itemWidth, eventQueue);
				row.addView(vg);
			}
			layout.addView(row);
		}
		
		// load the starting point
		int[]start = level.getStart();
		int[]target = level.getTarget();
		field[start[0]][start[1]].setStart(start[2]);
		field[target[0]][target[1]].setTarget(target[2]);

	}

}
